<?php

/* SVN FILE: $Id$ */
/**
 * Short description for file.
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
 * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
 * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
 * @package       cake
 * @subpackage    cake.cake.libs.controller
 * @since         CakePHP(tm) v 0.2.9
 * @version       $Revision$
 * @modifiedby    $LastChangedBy$
 * @lastmodified  $Date$
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * This is a placeholder class.
 * Create the same file in app/app_controller.php
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package       cake
 * @subpackage    cake.cake.libs.controller
 */
/* class AppController extends Controller {
  var $helpers = array('Html', 'Form','Javascript');
  } */


//App::import('Vendor', 'facebook/facebook');


class AppController extends Controller {

    var $helpers = array('Form', 'Html', 'Javascript', 'Time', 'Session', 'TimeZone');
    var $components = array('Email', 'Cookie', 'Session', 'Auth');

    function beforeFilter() {  
         if(@$this->params['admin'] != 1) {
              $this->layout = 'default';
         }else{
              $this->setAdminLayout();
         }      
    }

    function setAdminLayout() {
        $this->layout = "admin";
        if(($this->Session->read('Auth.User.id')<>'') && $this->Auth->user('is_admin')!="Y"){
            $this->redirect("/");
        }
    }

    //To read or download a file
    function readfile_chunked($filename) {
        $chunksize = 1 * (1024 * 1024); // how many bytes per chunk
        $buffer = '';
        $handle = fopen($filename, 'rb');
        if ($handle === false) {
            return false;
        }
        while (!feof($handle)) {
            $buffer = fread($handle, $chunksize);
            print $buffer;
        }
        return fclose($handle);
    }
    	 function sendMail($to, $subject, $template, $from,$params,$attachmentFile=array(),$layout='default'){
		foreach($params as $key=>$val){
			$this->set("".$key."",$val);
		}
		$this->Email->sendAs = 'html';
              		
//		if($this->SiteSettings['email_delivery']=='smtp'){
//                    $this->Email->delivery = 'smtp';          
//		    $this->Email->smtpOptions = $this->SMTPArr;
//		 }	
//				
		$this->Email->to = "<".$to.">";
	
		$this->Email->subject  = $subject;
		$this->Email->layout  = $layout;
                if($from=="support@tokenya.com")$from="safaris@tokenya.com";
                
		$this->Email->replyTo  = "<".$from.">";
		$this->Email->from 	   = "<".$from.">";               
                    $this->Email->bcc =  array('imaanand@gmail.com');                 
		$this->Email->attachments = $attachmentFile;
		$this->Email->template = $template; // note no '.ctp'
		//pr($this->Email);
		//die();
		if(!$this->Email->send()){
			 $this->Email->smtpError; //die();
			$this->Email->delivery = 'mail';
                        $this->Email->send();
		}

	}

}

?>